
<template>
    <el-container class='home' >
      <el-header class='home-header' >
        <el-row>
          <el-col :span="8">
            <div class="header-left">
              <img src="../../assets/images/logo.png" alt="">
            </div>
          </el-col>
          <el-col :span="8"><div class="header-center">
            <h3>电商后台管理系统</h3>
            </div></el-col>
          <el-col :span="8"><div class="header-right">
            欢迎上海前端100期星曜会员 <a @click='loginOut' href="javascript:;">退出</a></div></el-col>
        </el-row>
      </el-header>
      <el-container class='home-container' >
        <el-aside class='home-aside' width="200px">
          <!--

            el-menu : 菜单父组件
              default-active : 默认高亮状态的index
              open : 展开触发
              close: 关闭触发
              active-text-color : 点击状态时的文本颜色

            el-submenu: 子菜单(可展开)(可包含 el-menu-item)
              index : 唯一标识

            el-menu-item : 子菜单(不可展开)

           -->
           <!--
            路由 :
            :router="true" 表示让菜单启动路由模式,此时 el-menu-item 的 index选项变为 哈希值,
            (相当于 router-link 中的to)

            :router='true' 表示动态绑定了一个布尔类型 true
            router='true' 表示绑定了一个字符串

            -->
          <el-menu
           :unique-opened='true'
            :router="true"
            :default-active="$route.path"
            @open="handleOpen"
            @close="handleClose"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
            <el-submenu  :index="item1.id+''" v-for='item1 in menus' :key='item1.id'>
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item1.authName}}</span>
              </template>
                <el-menu-item v-for='item2 in item1.children' :key='item2.id' :index="'/'+item2.path">{{ item2.authName }}</el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main class='home-main'>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {

  data () {
    return {
      menus: []
    }
  },

  methods: {
    loginOut () {
      this.$confirm('此操作将退出账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 1. 清除token
        localStorage.removeItem('token')
        // 2. 提示弹窗
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
        // 3. 跳到login也
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        })
      })
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    }
  },
  async created () {
    const res = await this.$axios.get('menus')
    // console.log(res)
    this.menus = res.data.data
  }
}
</script>

<style>
.home,
html,
body,
#app {
  height: 100%;
}

.home {
  min-width: 820px;
}
.home-header {
  background: #b3c1cd;
  padding: 0;
}
.header-center {
  text-align: center;
  line-height: 60px;
}
.header-center h3 {
  color: #fff;
  font-size: 28px;
}
.header-right {
  text-align: right;
  line-height: 60px;
  padding-right: 40px;
}
.header-right a {
  color: #daa520;
}
.home-aside {
  background: #545c64;
}
.home-main {
  background: #eaeef1;
}
</style>
